//
//  TextDetailViewController.m
//  caishuitong
//
//  Created by qiysoft on 2017/4/21.
//  Copyright © 2017年 zhangxy. All rights reserved.
//

#import "TextDetailViewController.h"
//#import "CommentViewController.h"
#import "WZXQModel.h"
#import "NSMutableAttributedString+CalculateFrame.h"
#import "LoginViewController.h"
#import "WebHtmlCSS.h"

@interface TextDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate> {

    CGFloat webHight;
    WZXQModel *wZmodel;
    BOOL isHerf;
    CGFloat titHight;
    UIWebView *web1;
}

@property (weak, nonatomic) IBOutlet UILabel *lbCollect;
@property (weak, nonatomic) IBOutlet UILabel *lbSupport;
@property (weak, nonatomic) IBOutlet UILabel *lbComment;
@property (weak, nonatomic) IBOutlet UILabel *lbShare;
@property (strong, nonatomic) UIWebView *webView;

@property (weak, nonatomic) IBOutlet UIButton *btnOfCollect;
@property (weak, nonatomic) IBOutlet UIButton *btnOfSupport;

@end

@implementation TextDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (UIWebView *)webView {
    if (_webView == nil) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(5, 65 + 10, WSCREENH_WIDTH - 8, 300)];

        //        NSString *strHtml = [WebHtmlCSS allFormatTextHtml:wZmodel.strContent];
        //        NSString *strCss = [WebHtmlCSS makeUpFormatTextHtmlWithCSS];
        //        NSString *strHtmlCss = [NSString stringWithFormat:@"%@%@",strCss,strHtml];
        //
        //        NSData *data =[strHtmlCss dataUsingEncoding:NSUTF8StringEncoding];
        //        [_webView loadData:data MIMEType:@"text/html" textEncodingName:@"GBK" baseURL:nil];

        wZmodel.strContent = [@"<div style='text-align:justify;color:#606060;font-size:18px;line-height:1.8;letter-spacing: 1.5px;'>" stringByAppendingString: wZmodel.strContent];
        wZmodel.strContent = [wZmodel.strContent stringByAppendingString:@"</div>"];

        WLog(@"%@",@"href=\"/Data/\"");
        WLog(@"%@",@"href=\"http://www.csfw360.com/Data/\"");
        wZmodel.strContent = [wZmodel.strContent stringByReplacingOccurrencesOfString:@"href=\"/" withString:@"href=\"http://www.csfw360.com/"];
        wZmodel.strContent = [wZmodel.strContent stringByReplacingOccurrencesOfString:@"fgcontent" withString:@"fgcontentapp"];
        wZmodel.strContent = [wZmodel.strContent stringByReplacingOccurrencesOfString:@"index.php?m=Index&a=article&id=" withString:@"fgcontentapp/"];
        [_webView loadHTMLString:wZmodel.strContent baseURL:nil];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;

    }
    return _webView;
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    NSString * requestString = webView.request.URL.absoluteString;
    WLog(@"请求的地址：%@",requestString);

    if([requestString rangeOfString:@"http://www.csfw360.com/Data/Uploads/file/"].location !=NSNotFound)//_roaldSearchText
    {

        isHerf = YES;
        webView.hidden = YES;
        web1 = [[UIWebView alloc]init];
        web1.frame = CGRectMake(5, 65 + titHight, WSCREENH_WIDTH - 8, WSCREENH_HEIGHT - 64 - (65 + titHight));
        
//        webView.frame = CGRectMake(5, 65 + titHight, WSCREEN_WIDTH - 8, WSCREEN_HEIGHT - 49 - 64 - (65 + titHight));
        self.myTableView.frame = CGRectMake(0, 64, WSCREENH_WIDTH, WSCREENH_HEIGHT - 64);

        NSString *strings = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

        strings = [NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no'/></head><body>%@</body></html>",strings];
        strings = [strings stringByReplacingOccurrencesOfString:@"\"" withString:@"'"];
        [web1 loadHTMLString:strings baseURL:nil];
        
        [self.myTableView reloadData];

//        CGSize contentSize = webView.scrollView.contentSize; //设置内容板块的尺寸
//        CGSize viewSize = self.view.bounds.size;//自适应边界值
//        float sfactor = viewSize.width / contentSize.width;//调整因子计算
//
//        webView.scrollView.minimumZoomScale = sfactor;//最大调整参数设置为调整因子
//        webView.scrollView.maximumZoomScale = sfactor;//最小调整参数设置为调整因子
//        webView.scrollView.zoomScale = sfactor;  //设置本身无缩放，自适应

    }
    else
    {
        isHerf = NO;
        webHight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
        webView.frame = CGRectMake(5, 65 + titHight, WSCREENH_WIDTH - 8, webHight);
        self.myTableView.scrollEnabled = YES;
        [self.myTableView reloadData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    wZmodel = [WZXQModel mj_objectWithKeyValues:_dicData];
    if (!self.title) {
        NSString *strTitle;
        switch ([wZmodel.cid integerValue]) {
            case 24:
                strTitle = @"解读";
                break;
            case 16:
                strTitle = @"风险";
                break;
            case 17:
                strTitle = @"风险";
                break;
            case 18:
                strTitle = @"风险";
                break;
            case 19:
                strTitle = @"风险";
                break;
            case 28:
                strTitle = @"风险";
                break;
            case 29:
                strTitle = @"风险";
                break;
            case 30:
                strTitle = @"风险";
                break;
            case 31:
                strTitle = @"风险";
                break;
            case 3:
                strTitle = @"实务";
                break;
            case 56:
                strTitle = @"实务";
                break;
            case 57:
                strTitle = @"实务";
                break;
            case 58:
                strTitle = @"实务";
                break;
            case 14:
                strTitle = @"实务";
                break;
            case 15:
                strTitle = @"实务";
                break;
            case 25:
                strTitle = @"实务";
                break;
            case 52:
                strTitle = @"实务";
                break;
            case 267:
                strTitle = @"实务";
                break;
            case 268:
                strTitle = @"实务";
                break;
            case 269:
                strTitle = @"实务";
                break;
            case 270:
                strTitle = @"实务";
                break;
            case 23:
                strTitle = @"法规";
                break;
            case 35:
                strTitle = @"法规";
                break;
            case 36:
                strTitle = @"法规";
                break;
            case 37:
                strTitle = @"法规";
                break;
            case 38:
                strTitle = @"法规";
                break;
            case 46:
                strTitle = @"法规";
                break;
            case 47:
                strTitle = @"法规";
                break;
            case 32:
                strTitle = @"课程";
                break;
            case 33:
                strTitle = @"课程";
                break;
            default:
//                strTitle = @"法规";
                if ([wZmodel.cid integerValue] <= 233 && [wZmodel.cid integerValue] >= 56) {
                    strTitle = @"实务";
                }else if ([wZmodel.cid integerValue] <= 266 && [wZmodel.cid integerValue] >= 234) {
                    strTitle = @"税种";
                }else{
                    strTitle = @"详情";
                }
                break;
        }

        self.title = strTitle;
    }

    [self webView];
    //    [self.navigationController.navigationBar.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    wZmodel = [WZXQModel mj_objectWithKeyValues:_dicData];
    WLog(@"%@",wZmodel);

    if ([WShareUtil sharedData].dicSession) {
        [self requestCollectOrSupportState:[WShareUtil getServerURL:queryArticleMetaInfo]];
    }
}

//设置UITableViewCell的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identCell = @"HomeListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identCell];

    if (!cell) {
        cell = [[UITableViewCell alloc]init];

        UILabel *lbTitCell = [[UILabel alloc]init];
        lbTitCell.numberOfLines = 0;
        /*
         标题去空格转义
         */
        NSString *QKGString = [wZmodel.strTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];//(去空格)
        NSString *ZYString1 = [QKGString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
        NSString *ZYString2 = [ZYString1 stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];

        lbTitCell.text = ZYString2;
        lbTitCell.font = [UIFont systemFontOfSize:20];
        CGSize maximumLbTitle = CGSizeMake(WSCREENH_WIDTH - 28, MAXFLOAT);//labelsize的最大值
        CGSize expectSize = [lbTitCell sizeThatFits:maximumLbTitle];
        lbTitCell.frame = CGRectMake(13, 21, expectSize.width, expectSize.height);
        titHight = expectSize.height;
        
        // 标题高度自适应
        [UILabel changeSpaceForLabel:lbTitCell];
        [lbTitCell sizeToFit];
        
        // 时间
        UILabel *lbTimeCell = [[UILabel alloc]initWithFrame:CGRectMake(13, 30+ expectSize.height, 100, 30)];
        lbTimeCell.font = [UIFont systemFontOfSize:14];
        lbTimeCell.text = [WShareUtil timeFormat:wZmodel.createtime];
        lbTimeCell.textColor = WRGBColor(93, 93, 93);

        // 分割线
        UILabel *lbSourceCell = [[UILabel alloc]initWithFrame:CGRectMake(13, 63 + expectSize.height, WSCREENH_WIDTH-26, 1)];
        lbSourceCell.backgroundColor =[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
        
        // 阅读数
        UILabel *lbClickCell = [[UILabel alloc]initWithFrame:CGRectMake(WSCREENH_WIDTH - 143, 30 + expectSize.height, 130, 30)];
        lbClickCell.font = [UIFont systemFontOfSize:14];
        lbClickCell.text = [NSString stringWithFormat:@"%@次阅读",wZmodel.click];
        lbClickCell.textAlignment = NSTextAlignmentRight;
        lbClickCell.font = [UIFont systemFontOfSize:13];
        lbClickCell.textColor = WRGBColor(93, 93, 93);

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 70 + webHight + expectSize.height, WSCREENH_WIDTH - 20, 150)];
        imageView.image = [UIImage imageNamed:@"gzhpicture.png"];

        [cell addSubview:lbTitCell];
        [cell addSubview:lbSourceCell];
        [cell addSubview:lbTimeCell];
        [cell addSubview:lbClickCell];
        //        [cell addSubview:imageView];
        [cell addSubview:_webView];
        if (isHerf) {
            [cell addSubview:web1];
        }
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


//行点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}

//分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//列表行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

//设置行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (isHerf) {
        return WSCREENH_HEIGHT - 49 - 84;

    }else {
        return webHight + 65 + titHight;
    }

}

- (IBAction)btnOfClick:(UIButton *)sender {
    
}

// 收藏or点赞
- (void)requestData:(NSString *)url support:(NSString *)support{
    
}


// 请求点赞收藏状态
- (void)requestCollectOrSupportState:(NSString *)url {
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
/*
 //下面两行协助UIWebView 背景透明化，这两属性可以在 xib 中进行设置
 _webView.backgroundColor = [UIColor clearColor];
 //但是这个属性必须用代码设置，光xib 设置不行
 _webView.opaque = NO;

 //这行能在模拟器下明下加快loadHTMLString 后显示的速度，其实在真机上没有下句也感觉不到加载过程
 _webView.dataDetectorTypes = UIDataDetectorTypeNone;

 //下面的backgroud-color:transparent 结合最前面的两行代码指定的属性就真正使得 WebView 的背景透明了//而后的font:16px/18px 就是设置字体大小为 16px, 行间距为 18px，也可用  line-height: 18px 单独设置行间距
 //最后的Custom-Font-Name 就是前面在项目中加上的字体文件所对应的字体名称了
 NSString *webviewText = @"<style>body{margin:0;background-color:transparent;font:36px/88pxCustom-Font-Name}</style>";

 NSString *htmlString = [webviewText stringByAppendingFormat:@"%@",wZmodel.strContent];
 [_webView loadHTMLString:htmlString baseURL:nil];//在WebView 中显示本地的字符串
 */

